home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Help on Dynamically Allocating an Array?
- Date: 10 Jan 1996 12:55:28 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4d0d00$7aq@news.iag.net>
- References: <4cq273$aeq@mercury.IntNet.net> <4cvm5n$9au@gryphon.phoenix.net>
- NNTP-Posting-Host: pm1-orl23.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4cvm5n$9au@gryphon.phoenix.net>, brucew@phoenix.net says...
- >
- >jtomich@IntNet.net (Jeff Tomich) wrote:
- >
- >>I'm confused on who to dynamically allocate an array of any type. Any
- >>help would be much appreciated.
- >
- >char *p = malloc( numitems );
- >
- >
- >Bruce D. Wedding Have Compiler, Will Travel!
- > Perspicacious Programming Performed Promptly
- >Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
- >
-
- For any type T:
-
- T *ptr = malloc( numitems * sizeof(T));
- if( ptr == NULL)
- {
- /* allocation failed. handle it */
- }
-
- /* use the array */
- free( ptr); /* free the memory, when no longer needed */
-
- Because sizeof(char) == 1 by ansi definition, it is simpler to write the
- malloc for a char array without the "* sizeof(char)", as demonstrated
- by Bruce D. Wedding
-
- You might want to d/l and read through the c.l.c faq (Frequently Asked
- Question) list. If contains a considerable amount of useful information
- about pointers and dynamic allocation. It is available for anonymous ftp
- from rtfm.mit.edu /pub/usenet/comp.lang.c.
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-